<?php
namespace app\common\model;

use think\Model;
use hg\apidoc\annotation\Field;
use hg\apidoc\annotation\WithoutField;
use hg\apidoc\annotation\AddField;
use hg\apidoc\annotation\Param;

class JfArticle extends Model
{
    protected $table = 'jf_article';
    public function content(){
        return $this->hasOne(JfArticleContent::class,'id','content_id');
    }
    public function user(){
        return $this->hasOne(JfUser::class,'id','user_id');
    }
    /**
     * 保存新添加的掘粪文章
     */
    public function saveJfArticle($data){
        if (empty($data) || !is_array($data)) return false;
        $this->save($data);
        return $this->id;
    }
    /**
     * 更新掘粪文章
     */
    public function updateJfArticle($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update($data);
    }
    /**
     * 修改掘粪文章状态
     */
    public function statusJfArticle($data){
        if (empty($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['status' => $data['status']]);
    }
    /**
     * 修改掘粪文章审核状态
     */
    public function auditStatusJfArticle($data){
        if (empty($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['audit_status' => $data['auditStatus']]);
    }
    /**
     * 获取掘粪文章列表
     */
    public function getJfArticleList($data, $super_user_auth)
    {
        if (empty($data)) return false;
        $where = [
            ['status' ,'>' , $super_user_auth['id'] === 1 ? -20000 : -1],
            ['title', 'like', '%' . $data['title'] . '%']
        ];
        $order = [
            'id'=>'desc'
        ];
        $result = [
            'list' => $this->where($where)->limit($data['pageRow'])->page($data['pageIndex'])->order($order)->select(),
            'count' => $this->where($where)->order($order)->count()
        ];
        return $result;
    }
    /**
     * 获取掘粪文章列表用于展示
     */
    public function getNormalJfArticleList($data)
    {
        if (empty($data)) return false;
        $where = [
            ['status' ,'=',  1],
            ['audit_status' ,'=',  1]
        ];
        $order = [
            'id'=>'desc'
        ];
        $result = [
            'list' => $this->where($where)->limit($data['pageRow'])->page($data['pageIndex'])->order($order)->select(),
            'count' => $this->where($where)->order($order)->count()
        ];
        return $result;
    }
    /**
     * 获取掘粪文章详情
     */
    public function getNormalJfArticleById($data)
    {
        if (empty($data)) return false;
        $where = [
            ['status' ,'=',  1],
            ['audit_status' ,'=',  1],
            ['id' ,'=',  $data['id']]
        ];
        $result = $this->where($where)->find();
        return $result;
    }
}
